package com.sinoglobal.dumping.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.base.Dumpling_DateUtils;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyCashDumplingBean.MyCashDumpling;

public class Dumpling_MakeCashAdapter extends BaseAdapter {

	private List<MyCashDumpling> datas;
	private Context context;
	
	public Dumpling_MakeCashAdapter(Context context) {
		this.context = context ;
	}

	@Override
	public int getCount() {
		
		return datas == null ? 0  : datas.size();
	}

	@Override
	public Object getItem(int position) {

		if (datas != null ) {
			return datas.get(position);
		}
		
		return null ;
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null ;
		
		if (convertView == null ) {
			viewHolder = new ViewHolder() ;
			convertView = View.inflate(context, R.layout.dumpling_item_my_cash_dumpling, null);
			viewHolder.tv_make_cash_dumpling_content = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_content);
			viewHolder.tv_make_cash_dumpling_price = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_price);
			viewHolder.tv_make_cash_dumpling_date = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_date);
			viewHolder.tv_make_cash_dumpling_status = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_status);
			viewHolder.tv_make_cash_dumpling_number = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_number);
			viewHolder.ll_make_cash_dumpling_rootview = (LinearLayout) convertView.findViewById(R.id.ll_make_cash_dumpling_rootview);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
			
			MyCashDumpling myCashDumpling = datas.get(position);
			
			if (myCashDumpling.status == 3 || myCashDumpling.status == 1) {
				viewHolder.ll_make_cash_dumpling_rootview.setBackgroundColor(context.getResources().getColor(R.color.dumpling_white));
			} else {
				viewHolder.ll_make_cash_dumpling_rootview.setBackgroundColor(context.getResources().getColor(R.color.dumpling_gray6_e3e3e3));
			}
			
			String statusText = "" ;
			if (myCashDumpling.status == 3) {
				statusText = "未领完" ;
			} else if (myCashDumpling.status == 4) {
				statusText = "已领完" ;
			} else if (myCashDumpling.status == 5) {
				statusText = "已过期" ;
			} else if (myCashDumpling.status == 1) {
				statusText = "未发送" ;
			} 
			
			viewHolder.tv_make_cash_dumpling_content.setText(TextUtils.isEmpty(myCashDumpling.blessing) ? "恭喜发财,大吉大利" : myCashDumpling.blessing);
			viewHolder.tv_make_cash_dumpling_price.setText(myCashDumpling.moneyNum +"元");
			viewHolder.tv_make_cash_dumpling_date.setText(showTimeStyle(myCashDumpling.createDate));
			viewHolder.tv_make_cash_dumpling_status.setText(statusText);
			viewHolder.tv_make_cash_dumpling_number.setText(myCashDumpling.receiveNum+ "/" + myCashDumpling.dumplinglNum + "个");
		return convertView;
	}

	public void setData(List<MyCashDumpling> datas) {
		this.datas = datas ;
		notifyDataSetChanged();
	}

	public static class ViewHolder {
		private TextView  tv_make_cash_dumpling_content ;
		private TextView  tv_make_cash_dumpling_price ;
		private TextView  tv_make_cash_dumpling_date ;
		private TextView  tv_make_cash_dumpling_status ;
		private TextView  tv_make_cash_dumpling_number ;
		private LinearLayout ll_make_cash_dumpling_rootview ;
	}

	/**
	 * 加载 更多的数据
	 * @param resultList
	 */
	public void setDataList(List<MyCashDumpling> resultList) {
		this.datas.addAll(resultList);
		notifyDataSetChanged();
		
	}
	
	/**
	 * 距当前时间一小时内，显示为xx分钟前
	 */
	private long oneHourTime = 60*60*1000 ;
	/**
	 * 小于24小时，显示为xx小时前
	 */
	private long oneDayTime = oneHourTime * 24 ;
	/**
	 * 间隔超过24小时，显示：昨天xx：xx   间隔超过48小时，显示：xx月xx日
	 */
	private long twoDayTime = oneDayTime * 2 ;
	/**
	 * 间隔超一年，显示：20xx年 xx月xx日
	 */
	private long oneYearTime = oneDayTime * 365 ;
	
	private String showTimeStyle(long createTime){
		String result = "" ;
		long current = Dumpling_DateUtils.getCurrentTime();
//		long current = System.currentTimeMillis() ;
		long afterTime = System.currentTimeMillis() - createTime ;
		if ( afterTime - oneHourTime < 0) {
			if (afterTime/(1000*60) <= 0) {
				result = "刚刚" ;
			} else {
				result = afterTime/(1000*60) + "分钟前" ;
			}
		} else if (afterTime - oneDayTime < 0) {
			result = afterTime/oneHourTime + "小时前" ;
		} else if (afterTime - twoDayTime < 0) {
			if (Long.parseLong(Dumpling_DateUtils.format(current, "dd")) - Long.parseLong(Dumpling_DateUtils.format(createTime, "dd")) >= 2) {
				result = Dumpling_DateUtils.format(createTime, "MM月dd日") ;
			} else {
				result = "昨天" + Dumpling_DateUtils.format(createTime, "HH:mm") ;
			}
		} else if (afterTime - oneYearTime < 0) {
			if (Long.parseLong(Dumpling_DateUtils.format(current, "yyyy")) - Long.parseLong(Dumpling_DateUtils.format(createTime, "yyyy")) >= 1) {
				result = Dumpling_DateUtils.format(createTime, "yyyy年MM月dd日") ;
			} else {
				result = Dumpling_DateUtils.format(createTime, "MM月dd日") ;
			}	
		} else {
			result = Dumpling_DateUtils.format(createTime, "yyyy年MM月dd日") ;
		}
		
		return result ;
	}
	
}
